﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;

namespace SusuCMS
{
    public class CmsDataContext : DbContext, IUnitOfWork
    {
        public DbSet<Website> Websites { get; set; }

        public DbSet<PageType> PageTypes { get; set; }

        public DbSet<Page> Pages { get; set; }

        public DbSet<User> Users { get; set; }

        public DbSet<PageImage> PageImages { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().ToTable("User");
            modelBuilder.Entity<Website>().ToTable("Website");
            modelBuilder.Entity<PageType>().ToTable("PageType");
            modelBuilder.Entity<Page>().ToTable("Page");
            modelBuilder.Entity<PageImage>().ToTable("PageImage");

            modelBuilder.Entity<PageType>()
                .HasOptional(i => i.Parent)
                .WithMany(i => i.Children)
                .HasForeignKey(i => i.ParentId);
        }

        public void Save()
        {
            SaveChanges();
        }
    }
}
